package org.jeecg.modules.project.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.project.entity.BimProjectFloor;
import org.jeecg.modules.project.service.IBimProjectFloorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: bim_project_floor
 * @Author: jeecg-boot
 * @Date: 2023-04-13
 * @Version: V1.0
 */
@Api(tags = "楼层管理")
@RestController
@RequestMapping("/bim/floor")
@Slf4j
public class BimProjectFloorController extends JeecgController<BimProjectFloor, IBimProjectFloorService> {

    @Autowired
    private IBimProjectFloorService bimProjectFloorService;

    /**
     * 分页列表查询
     *
     * @param bimProjectFloor
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "楼层分页列表查询")
    @ApiOperation(value = "楼层分页列表查询", notes = "楼层分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BimProjectFloor bimProjectFloor,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BimProjectFloor> queryWrapper = QueryGenerator.initQueryWrapper(bimProjectFloor, req.getParameterMap());
        Page<BimProjectFloor> page = new Page<BimProjectFloor>(pageNo, pageSize);
        IPage<BimProjectFloor> pageList = bimProjectFloorService.page(page, queryWrapper);
        if (CollUtil.isNotEmpty(pageList.getRecords())) {
            List<BimProjectFloor> projectFloorList = pageList.getRecords().stream().sorted(new Comparator<BimProjectFloor>() {
                @Override
                public int compare(BimProjectFloor o1, BimProjectFloor o2) {
                    if (Integer.valueOf(o1.getFloorNo()) > Integer.valueOf(o2.getFloorNo())) {
                        return 1;
                    } else if (Integer.valueOf(o1.getFloorNo()).equals(Integer.valueOf(o2.getFloorNo()))) {
                        return 0;
                    } else {
                        return -1;
                    }
                }
            }).collect(Collectors.toList());
            pageList.setRecords(projectFloorList);
        }
        return Result.OK(pageList);
    }

    /**
     * 楼层添加
     *
     * @param bimFloorList
     * @return
     */
    @AutoLog(value = "楼层添加")
    @ApiOperation(value = "楼层添加", notes = "楼层添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Valid List<BimProjectFloor> bimFloorList) {
        return Result.OK(bimProjectFloorService.add(bimFloorList));
    }

    /**
     * 楼层编辑
     *
     * @param bimProjectFloor
     * @return
     */
    @AutoLog(value = "楼层编辑")
    @ApiOperation(value = "楼层编辑", notes = "楼层编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BimProjectFloor bimProjectFloor) {
        bimProjectFloorService.edit(bimProjectFloor);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "楼层通过id删除")
    @ApiOperation(value = "楼层通过id删除", notes = "楼层通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        bimProjectFloorService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "楼层批量删除")
    @ApiOperation(value = "楼层批量删除", notes = "楼层批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.bimProjectFloorService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "楼层通过id查询")
    @ApiOperation(value = "楼层通过id查询", notes = "楼层通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BimProjectFloor bimProjectFloor = bimProjectFloorService.getById(id);
        if (bimProjectFloor == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(bimProjectFloor);
    }

}
